package com.collectionAndMap.collection.set;

import java.util.Comparator;
import java.util.TreeSet;

/***
 * @ClassName: TreeSetComparator
 * @Description: 定制比较器的TreeSet
 * @Auther: 送一一块二向箔
 * @Date: 16:58 2022/2/17
 * @version : V1.0
 */
public class TreeSetComparator {
    public static void main(String[] args) {
        //先比年龄再比名字
        TreeSet<Boy> boy = new TreeSet<>(new Comparator<Boy>() {
            @Override
            public int compare(Boy o1, Boy o2) {
                int i1 = o1.getAge()-o2.getAge();
                int i2 = o1.getName().compareTo(o2.getName());
                return i1==0?i2:i1;
            }
        });

        //使用lambda实现

        TreeSet<Boy> set = new TreeSet<>((o1,o2)->{
            int i1 = o1.getAge()-o2.getAge();
            int i2 = o1.getName().compareTo(o2.getName());
            return i1==0?i2:i1;
        });

        Boy b1 = new Boy("aca", 3);
        Boy b2 = new Boy("aba", 4);
        Boy b3 = new Boy("ccc", 1);
        set.add(b1);
        set.add(b2);
        set.add(b3);
        System.out.println(set.toString());
    }
}
